//
//  IdentityChangeViewController.m
//  tryshopping.boss
//
//  Created by Mr.Lee on 2017/3/24.
//  Copyright © 2017年 Reabam. All rights reserved.
//


#define KPFooterHeight 10           // cell间距
#define KPCellHeight 60             // cell高度
#define KPConfigBtnHeight 43        // 确认按钮高度
#define KPConfigBtnLeftPading 10    // 确认按钮高度
#define KPConfigBtnTopPading 37     // 确认按钮高度
#import "IdentityChangeViewController.h"
#import "IdentityChangeCell.h"              // 身份变更cell
#import "StoreRolesListModel.h"             // 身份列表模型

@interface IdentityChangeViewController () <UITableViewDelegate, UITableViewDataSource>
/** tableView */
@property (nonatomic, weak) UITableView *tableV;
// 记录上一次索引
@property(nonatomic,strong)NSIndexPath *lastPath;
/** 身份名称 */
@property (nonatomic, copy) NSString *identiTypeName;
/** 身份类型 */
@property (nonatomic, copy) NSString *identiType;
/** 数据数组 */
@property (nonatomic, strong) NSMutableArray *modelArray;
/** 确定按钮 */
@property (nonatomic, weak) UIButton *configBtn;
@end

@implementation IdentityChangeViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"身份变更";
    
    // INIT UI
    [self setUpUI];
    
    // 初始化数据数组
    self.modelArray = [NSMutableArray array];
    
    // 请求数据
    [self requestData];
}

#pragma mark -- 初始化UI
- (void)setUpUI {
    UITableView *tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64) style:UITableViewStylePlain];
    tableV.backgroundColor = zuiqianhuiColor;
    self.tableV = tableV;
    tableV.delegate = self;
    tableV.dataSource = self;
    tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableV];
    
    UIButton *configBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [configBtn setTitle:@"确定" forState:UIControlStateNormal];
    [configBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    configBtn.titleLabel.font = [UIFont systemFontOfSize:17.0];
    [configBtn setBackgroundColor:CommonColor];
    configBtn.layer.cornerRadius = 2.0;
    configBtn.layer.masksToBounds = YES;
    self.configBtn = configBtn;
    [configBtn addTarget:self action:@selector(configBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.tableV addSubview:configBtn];
    
    tableV.tableFooterView = [[UIView alloc] init];
}

#pragma mark -- 网络请求
- (void)requestData{
    NSString *Token = [UserDefaults objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
//    NSLog(@"-----params---- %@", params);
    [MBProgressHUD showIndeterminateHudSub:@"加载中，请稍候。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"manage/findRoles" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"findRolesListm====%@",severDataDict.dataDict);
//        plistFileName(@"findRolesListm");
        if (isSuccess) {
            [self.modelArray removeAllObjects];
            [MBProgressHUD hideProgressHUDInternal:self.view];
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                NSMutableArray *dataArr = [severDataDict.dataDict objectForKey:@"roles"];
                for (NSDictionary* dicc in dataArr ){
                    StoreRolesListModel *model = [StoreRolesListModel mj_objectWithKeyValues:dicc];
                    [self.modelArray addObject:model];
                }
                [self.tableV reloadData];
                
                self.configBtn.frame = CGRectMake(KPConfigBtnLeftPading, KPCellHeight * self.modelArray.count + KPFooterHeight * self.modelArray.count + KPConfigBtnTopPading, ScreenWidth - KPConfigBtnLeftPading * 2, KPConfigBtnHeight);
               
                
            } else {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    LoginViewController *loginVC = [[LoginViewController alloc] init];
                    [self.navigationController pushViewController:loginVC animated:YES];
                }
            }
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
    
}

#pragma mark -- 点击确定按钮
- (void)configBtnClick{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    [params setValue:self.scId forKey:@"scId"];
//    [params setValue:self.identiTypeName forKey:@"userTypeName"];
    [params setValue:self.identiType forKey:@"userType"];
//    NSLog(@"params ==== %@",params);
    [HttpRequest requestData:@"manage/editorRole" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
//            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                [MBProgressHUD showTextHudSub:@"修改成功" view:self.view];
                if (self.IdentityChangeViewControllerBlock) {
                    self.IdentityChangeViewControllerBlock(self.identiType);
                    
                }
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                
                [MBProgressHUD showTextHud:requestResultStr view:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    LoginViewController* Login=[[LoginViewController alloc]init];
                    [self.navigationController pushViewController:Login animated:YES];
                    
                }
            }
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}

#pragma mark -- UITableView DataSource
// 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.modelArray.count;
}
// 每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//每行的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  KPCellHeight;
}

// 返回每行cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    StoreRolesListModel *model = self.modelArray[indexPath.section];
    static NSString *CellIdentifier = @"IdentityChangeCell";
    IdentityChangeCell *cell = ( IdentityChangeCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"IdentityChangeCell" owner:nil options:nil] lastObject];
    }
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.selectBtn setUserInteractionEnabled:NO];
    
    // 设置选中和第一次默认选中
    NSInteger row = [indexPath section];
    NSInteger oldRow = [self.lastPath section];
    if (self.lastPath!=nil) {
        
        if (row == oldRow) {
            cell.selectBtn.selected = YES;
        } else{
            cell.selectBtn.selected = NO;
        }
        
    } else {
        if (self.titleName.length > 0) {
            if ([self.titleName isEqualToString:model.userTypeName]) {
                cell.selectBtn.selected = YES;
//                NSString *identiTypeName = self.titleName;
                self.lastPath = indexPath;
                self.identiType = self.userType;
                self.identiTypeName = self.titleName;
                NSLog(@"-----%@--%@------",self.identiType, self.identiTypeName);
            } else{
                cell.selectBtn.selected = NO;
            }
            
        } else {
            if (indexPath.section == 0) {
                cell.selectBtn.selected = YES;
                StoreRolesListModel *model = self.modelArray[0];
                NSString *identiTypeName = model.userTypeName;
                NSString *identiType = model.userType;
                
                self.identiTypeName = identiTypeName;
                self.identiType = identiType;
                NSLog(@"-----%@ -- %@------", identiType, identiTypeName);
                self.lastPath = indexPath;
            } else{
                cell.selectBtn.selected = NO;
            }
        }
        
    }
    return cell;
    
}

// 返回每行的header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width,KPFooterHeight)];
    [view setBackgroundColor:zuiqianhuiColor];
    return view;
}
// 返回每行的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return KPFooterHeight;
}


#pragma mark -- UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 单选
    NSInteger newRow = [indexPath section];
    NSInteger oldRow = (self.lastPath !=nil)?[self.lastPath section]:-1;
    if (newRow != oldRow) {
        StoreRolesListModel *model = self.modelArray[indexPath.section];
        IdentityChangeCell *newCell = ( IdentityChangeCell *)[tableView cellForRowAtIndexPath:indexPath];
        newCell.selectBtn.selected = YES;
        self.identiTypeName = model.userTypeName;
        self.identiType = model.userType;
        IdentityChangeCell *oldCell = ( IdentityChangeCell *)[tableView cellForRowAtIndexPath:self.lastPath];
        oldCell.selectBtn.selected = NO;
        self.lastPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark --  LifeCycle
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
}
@end




